package com.example.hellonotes.activity;


import com.example.hellonotes.R;
import com.example.hellonotes.fragment.FragmentController;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends FragmentActivity implements
OnCheckedChangeListener, OnClickListener{

	
	private RadioGroup rg_tab;
	private ImageView iv_add;
	private FragmentController controller;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		controller = FragmentController.getInstance(this, R.id.fl_content);
		controller.showFragment(0);
		
		initView();
	}
	
	private void initView() {
		rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
		iv_add = (ImageView) findViewById(R.id.iv_add);
		rg_tab.setOnCheckedChangeListener(this);
		iv_add.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO 自动生成的方法存根
		switch (v.getId()) {
		case R.id.iv_add:
			Intent intent = new Intent(MainActivity.this, AddActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	/**
	 * 通过RadioGroup的4个按钮来切换显示4个碎片
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		
		switch (checkedId) {
		case R.id.rb_home:
			controller.showFragment(0);
			break;
		case R.id.rb_meassage:
			controller.showFragment(1);
			break;
		case R.id.rb_search:
			controller.showFragment(2);
			break;
		case R.id.rb_user:
			controller.showFragment(3);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		FragmentController.onDestroy();
	}

}

/**
 * 原MainActivity 代码
 */

/*public class MainActivity extends SlidingFragmentActivity implements OnClickListener{

	private User user;
	private NotesAdapter notesAdapter;
	private ListView lv;
	private Intent i;
	private NotesDB notesDB;
	private SQLiteDatabase dbReader;
	private Cursor cursor;
	private SlidingMenu mSlidingMenu;
	private NaviFragment naviFragment;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initTitle();
		initView();
		initFragment();
		//user = application.currentUser;


		//System.out.println(user);


		

	}
	
	
		public void initFragment() {
		
		mSlidingMenu = getSlidingMenu();
		setBehindContentView(R.layout.frame_navi); // 给滑出的slidingmenu的fragment制定layout

		mSlidingMenu.setMode(SlidingMenu.LEFT);// 设置slidingmeni从哪侧出现

		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);// 只有在边上才可以打开

		mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// 偏移量

		mSlidingMenu.setFadeEnabled(true);
		mSlidingMenu.setFadeDegree(0.5f);
		mSlidingMenu.setMenu(R.layout.frame_navi);
		naviFragment = new NaviFragment();
		getSupportFragmentManager().beginTransaction()
		.replace(R.id.frame_navi, naviFragment).commit();
	}
	
	
	
	*//**

	 * 该方法初始化xml控件

	 *//*
	
	public void initView() {
		
		
		lv = (ListView)findViewById(R.id.list);
		notesDB = new NotesDB(this);
		dbReader = notesDB.getReadableDatabase();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO 自动生成的方法存根


				cursor.moveToPosition(position);
				i = new Intent(MainActivity.this,selectActivity.class);
				i.putExtra(NotesDB.ID, cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
				i.putExtra(NotesDB.CONTENT, cursor.getString(cursor.getColumnIndex(NotesDB.CONTENT)));
				i.putExtra(NotesDB.TIME, cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
				i.putExtra(NotesDB.PATH, cursor.getString(cursor.getColumnIndex(NotesDB.PATH)));
				startActivity(i);
			}
		});
	}
	
	*//**

	 * 初始化标题栏

	 *//*
	private void initTitle() {
		new TitleBuilder(this).setTitleText("记事本")
		.setLeftImage(R.drawable.message_add_background)
		.setLeftOnClickListener(this)
		.setRightImage(R.drawable.message_add_background_highlighted)
		.setRightOnClickListener(this);
		
	}
	
	*//**

	 * 获取当前登录用户的信息

	 *//*
	
	
	*//**

	 * 退出用户登录

	 *//*
	public void quitUser() {
		
		BmobUser.logOut(this);   //清除缓存用户对象

		BmobUser currentUser = BmobUser.getCurrentUser(this); // 现在的currentUser是null了

	}

	
	
	public void selectDB() {
		cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null);
		notesAdapter = new NotesAdapter(this, cursor);
		lv.setAdapter(notesAdapter);
	}
	
	
	
	@Override
	protected void onResume() {
		super.onResume();
		//fetchData();


		selectDB();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titlebar_iv_right:
			i = new Intent(MainActivity.this,AddActivity.class);
			startActivity(i);
			break;
		case R.id.titlebar_iv_left:
			i = new Intent(MainActivity.this,MainActivity2.class);
			startActivity(i);
			//mSlidingMenu.toggle();


			break;
		}
	}
	

}*/
